<template>
    <div class="client-nursing-container">
        <h2 class="title">护理记录</h2>

        <!-- 查询表单 -->
        <el-form :inline="true" :model="searchForm" class="search-form">
            <div class="search-left">
                <el-form-item label="客户名:">
                    <el-input v-model="searchForm.name" placeholder="请输入客户名" clearable />
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="handleSearch">查询</el-button>
                    <el-button @click="handleReset">重置</el-button>
                </el-form-item>
            </div>

        </el-form>

        <!-- 客户信息表格 -->
        <el-table :data="tableData" v-loading="loading" border style="width: 100%; background: #f5f7fa;"
            :header-cell-style="{ background: '#f9fafc', color: '#333', textAlign: 'center' }"
            :cell-style="{ textAlign: 'center' }">
            <el-table-column prop="name" label="客户名" />
            <el-table-column prop="gender" label="性别" width="80" />
            <el-table-column prop="age" label="年龄" width="80" />
            <el-table-column prop="phone" label="电话" />
            <el-table-column prop="idNumber" label="身份证号" />
            <el-table-column prop="bloodType" label="血型" width="100" />
            <el-table-column label="操作" width="180">
                <template #default="scope">
                    <el-button type="primary" size="small" @click="viewRecord(scope.row.clientId)">查看护理记录</el-button>
                </template>
            </el-table-column>
        </el-table>

        <!-- 分页 -->
        <div class="pagination-bar">
            <el-pagination v-model:current-page="pagination.cur" v-model:page-size="pagination.size"
                :total="pagination.total" :page-sizes="[5, 10, 20]" layout="total, sizes, prev, pager, next, jumper"
                @current-change="handleCurrentChange" @size-change="handleSizeChange" />
        </div>

        <NursingRecordTable ref="recordRef" />
    </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import {inject} from 'vue'
const axios = inject("axios")
import { ElMessage } from 'element-plus'
import NursingRecordTable from '@/components/nursing/clientNursing/NursingRecordTable.vue'

const searchForm = ref({ name: '' })
const tableData = ref([])
const loading = ref(false)
const pagination = ref({ cur: 1, size: 5, total: 0 })

const recordRef = ref()

const handleSearch = () => {
    pagination.value.cur = 1
    query(1)
}

const handleReset = () => {
  searchForm.value.name = ''
  pagination.value.cur = 1
  query(1)
}

const query = async (page) => {
    loading.value = true
    try {
        const res = await axios.get('/client/pageClient', {
            params: {
                cur: page,
                number: pagination.value.size,
                name: searchForm.value.name
            }
        })
        if (res.data.status === 200) {
            tableData.value = res.data.data || []
            pagination.value.total = res.data.total || 0
        } else {
            ElMessage.warning(res.data.msg || '查询失败')
        }
    } finally {
        loading.value = false
    }
}

const handleCurrentChange = (page) => {
    pagination.value.cur = page
    query(page)
}

const handleSizeChange = (size) => {
    pagination.value.size = size
    pagination.value.cur = 1
    query(1)
}

const viewRecord = (clientId) => {
    recordRef.value.open(clientId)
}

onMounted(() => {
    query(1)
})
</script>

<style scoped>
.client-nursing-container {
    padding: 20px;
}

.title {
    font-size: 27.2px;
    font-weight: bold;
    margin: 0 0 20px 0;
    color: #333;
}

.search-form {
    display: flex;
    justify-content: flex-start;
    flex-wrap: wrap;
    margin-bottom: 20px;
}

.search-left {
    display: flex;
    flex-wrap: wrap;
    gap: 12px;
}

.pagination-bar {
    display: flex;
    justify-content: flex-end;
    margin-top: 2%;
    margin-bottom: 2%;
}
</style>
